package com.example.mqspring.rabiitmqbasicleverone.rabbitmqBroadcast;

import com.rabbitmq.client.Channel;

import java.io.IOException;

/**
 * java广播消息发布
 */
public class MessagerPublisher {

    public void sendMessage(Channel channel,String myExchangge,String message,String...queue){
        try {
//           申明一个广播的交换器
            channel.exchangeDeclare(myExchangge,"fanout");
            bindQueue(channel,myExchangge,queue);
//            发送消息
            channel.basicPublish(myExchangge, "", null,message.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void bindQueue(Channel channel,String myExchangge,String...queue) {
        try {
            for (String que:queue) {
                channel.queueDeclare(que,false,false,false,null);
                channel.queueBind(que,myExchangge,"");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
